<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/15
 * Time: 9:16
 */

namespace App\Console\Service\IPProxyPool;

use App\Console\Service\IPProxyPool\Process\Download;
use App\Console\Service\IPProxyPool\Process\Validate;

class PrimaryService
{
    public function run()
    {
        $this->download();
    }

    /**
     * 进程池管理
     */
    private function work()
    {
        $workerNum = 2;
        $pool = new \Swoole\Process\Pool($workerNum);
        $pool->on("WorkerStart", function ($pool, $workerId) {
            switch ($workerId) {
                case 0:
                    echo $workerId . PHP_EOL;
                    sleep(5);
                    $this->download();
                    break;
                case 1:
                    echo $workerId . PHP_EOL;
                    sleep(5);
                    $this->validate();
                    break;
                default:
                    return;
            }
        });
        $pool->on("WorkerStop", function ($pool, $workerId) {
            echo "Worker#{$workerId} is stopped\n";
        });
        $pool->start();
    }

    /**
     * 下载器
     */
    private function download()
    {
        $download = new Download();
        $download->run();
    }

    /**
     * 验证器
     */
    private function validate()
    {
        $Validate = new Validate();
        $Validate->run();
    }
}